package com.indexingsystem.system.controller;


import com.indexingsystem.boss.Vo.ExportPahtUpdateReqVo;
import com.indexingsystem.boss.Vo.ExportPathAddReqVo;
import com.indexingsystem.boss.Vo.ExportPathDeleteReqVo;
import com.indexingsystem.boss.Vo.ExportPathReqVo;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.common.ProgramLogsCommon;
import com.indexingsystem.boss.entity.IdxExportPath;
import com.indexingsystem.boss.entity.ProgramLogs;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.DateUtil;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.system.exception.ServiceResultException;
import com.indexingsystem.system.service.IProgramLogsService;
import com.indexingsystem.system.service.IdxExportPathService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/IndexingSystem/Service")
public class ExportFilePathController {

    private ProgramLogs logs;
    @Resource
    private IProgramLogsService logsService;
    @Resource
    private IdxExportPathService idxExportPathService;

    /**
     * 7.1 新增导出文件路径
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/ExportPath/addExportPath", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addExportPath(@RequestBody ExportPathAddReqVo reqVo) {
        LogCvt.info("/Service/ExportPath/addExportPath");
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo) {
            LogCvt.info(reqVo.toString());
            try {
                if (reqVo.getPathName().length() > 32) {
                    message = "路径名称必须小于32个字符！";
                } else {
                    Integer i = idxExportPathService.insertSelective(reqVo);
                    message = "新增导出路径成功！";
                    result_code = "0000";
                }

            } catch (ServiceResultException soe) {
                LogCvt.error("新增导出路径失败，原因：" + soe.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/ExportPath/addExportPath");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(soe.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + soe.getMessage();
            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        LogCvt.info(result.toString());
        return result;
    }

    /**
     * 7.2 修改导出文件路径
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/ExportPath/updateExportPath", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateExportPath(@RequestBody ExportPahtUpdateReqVo reqVo) {
        LogCvt.info("/Service/ExportPath/updateExportPath");
        Map<String, Object> result = new HashMap<>();
        String message = "操作失败";
        String result_code = "9999";
        if (null != reqVo && StringUtils.isNotBlank(reqVo.getPathCode())) {
            LogCvt.info(reqVo.toString());
            try {
                if ( reqVo.getPathName() != null && reqVo.getPathName().length() > 32) {
                    message = "路径名称必须小于32个字符！";
                } else {
                    int i = idxExportPathService.updateByPrimaryKeySelective(reqVo);
                    if (i == 0) {
                        message = "该路径不存在！";
                    } else if (i > 0) {
                        message = "修改导出路径成功！";
                        result_code = "0000";
                    }
                }
            } catch (ServiceResultException soe) {
                LogCvt.error("更新导出路径失败，原因：" + soe.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/ExportPath/updateExportPath");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(soe.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + soe.getMessage();
            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        LogCvt.info(result.toString());
        return result;
    }

    /**
     * 7.3 批量删除导出文件路径
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/ExportPath/deleteExportPath", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteExportPath(@RequestBody ExportPathDeleteReqVo reqVo) {
        LogCvt.info("/Service/ExportPath/deleteExportPath");
        Map<String, Object> result = new HashMap<>();
        LogCvt.info(reqVo.toString());
        List<String> ids = reqVo.getIds();
        String message = "操作失败";
        String result_code = "9999";
        if (ids != null && ids.size() > 0) {
            LogCvt.info(ids.toString());
            try {
                int statusOk = 0;
                if (ids == null || ids.size() < 1) {
                    message = "删除路径为空！";
                } else if (idxExportPathService.selectByPrimaryKeyList(ids) < ids.size()) {
                    message = "路径不存在！";
                } else if ((statusOk = idxExportPathService.selectByStatusOk(ids)) > 0) {
                    message = "有" + statusOk + "个路径正在启用，无法删除！";
                } else {
                    idxExportPathService.deleteByPathCodeList(ids);
                    message = "删除导出路径成功！";
                    result_code = "0000";
                }

            } catch (Exception ex) {
                LogCvt.error("批量删除导出路径失败，原因：" + ex.getMessage());
                logs = ProgramLogsCommon.init();
                logs.setMessage("/Service/ExportPath/deleteExportPath");
                logs.setLevel("1");
                logs.setLogger("ToDataBase");
                logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
                logs.setException(ex.toString());
                logsService.insertSelective(logs);

                message = "操作失败：" + ex.getMessage();

            }
        } else {
            message = "参数不全！";
            result_code = "6666";
        }
        result.put("message", message);
        result.put("result_code", result_code);
        LogCvt.info(result.toString());
        return result;
    }

    /**
     * 7.4 导出文件路径列表查询
     * @param reqVo
     * @return
     */
    @RequestMapping(value = "/Synonym/getExportPathList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getExportPathList(@RequestBody ExportPathReqVo reqVo) {
        LogCvt.info("/Service/Synonym/getExportPathList");
        Map<String, Object> result = new HashMap<>();
        PagedResult<IdxExportPath> pagedResult = new PagedResult<>();
        String message = "操作失败";
        String result_code = "9999";
        String reason = "";
        try {
            pagedResult = idxExportPathService.getExportPathList(reqVo);
            result.put("result", pagedResult.getDataList());
            result.put("length", pagedResult.getTotal());
            result.put("pageNumber", pagedResult.getPages());
            message = "操作成功！";
            result_code = "0000";
        }  catch (ParseException ex) {
            if (ex instanceof ParseException) {
                reason = "日期格式错误！";
            } else {
                reason = ex.getMessage();
            }
            LogCvt.error("导出文件路径列表查询失败，原因：" + reason);
            logs = ProgramLogsCommon.init();
            logs.setMessage("/Service/ExportPath/deleteExportPath");
            logs.setLevel("1");
            logs.setLogger("ToDataBase");
            logs.setLogTime(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
            logs.setException(ex.toString());
            logsService.insertSelective(logs);

            message = "操作失败：" + reason;

        }
        result.put("message", message);
        result.put("result_code", result_code);
        LogCvt.info(result.toString());
        return result;
    }
}
